package com.sunyard.domee.handler;


import com.sunyard.domee.common.enums.CouponStatusEnum;
import com.sunyard.domee.common.enums.DelFlagEnum;
import com.sunyard.domee.common.model.PageListResult;
import com.sunyard.domee.commons.constants.CoreConstants;
import com.sunyard.domee.dal.query.MarketingCouponQuery;
import com.sunyard.domee.service.MarketingCouponService;
import com.sunyard.domee.service.bo.MarketingCouponBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @program: member
 * @description: WashCarService
 * @author: raojinyx@163.com
 * @create: 2020-05-25 20:02
 **/
@Service
@Slf4j
public class MarketingCouponHandler {
    @Autowired
    private MarketingCouponService marketingCouponService;


    /**
     * 分页查询
     * @param condition
     * @return
     */
    public PageListResult<MarketingCouponBO> findPage(MarketingCouponQuery condition) {
        if (ObjectUtils.isEmpty(condition)) {
            log.warn("findPage 参数有误，{}", CoreConstants.GSON.toJson(condition));
            return new PageListResult();
        }
        condition.setDelFlag(DelFlagEnum.NOT_DEL.getCode());
        PageListResult<MarketingCouponBO> page = marketingCouponService.findPage(condition);
        return page;
    }

    /**
     * List 查询
     * @param washCarServiceBO
     * @return
     */
    public List<MarketingCouponBO> findList(MarketingCouponQuery washCarServiceBO) {
        if (ObjectUtils.isEmpty(washCarServiceBO)) {
            log.warn("findList 参数有误，{}", CoreConstants.GSON.toJson(washCarServiceBO));
            return new ArrayList<>();
        }
        washCarServiceBO.setDelFlag(DelFlagEnum.NOT_DEL.getCode());
        return marketingCouponService.findList(washCarServiceBO);
    }

    /**
     * List 查询启用有效的优惠券
     * @param washCarServiceBO
     * @return
     */
    public List<MarketingCouponBO> findListValid(MarketingCouponQuery washCarServiceBO) {
        if (ObjectUtils.isEmpty(washCarServiceBO)) {
            log.warn("findList 参数有误，{}", CoreConstants.GSON.toJson(washCarServiceBO));
            return new ArrayList<>();
        }
        washCarServiceBO.setCouponStatus(CouponStatusEnum.ENABLE.getCode());
        washCarServiceBO.setFindDate(new Date());
        return this.findList(washCarServiceBO);
    }


    /**
     * 主键ID查询
     * @param id
     * @return
     */
    public MarketingCouponBO findById(Long id) {
        if (ObjectUtils.isEmpty(id)) {
            log.warn("findList 参数有误，{}", CoreConstants.GSON.toJson(id));
            return null;
        }
        MarketingCouponBO washCarServiceBO = marketingCouponService.findById(id);
        return washCarServiceBO;
    }

    /**
     * 新增
     * @param marketingCouponBO
     * @return
     */
    public MarketingCouponBO add(MarketingCouponBO marketingCouponBO) {
        marketingCouponBO.setDelFlag(DelFlagEnum.NOT_DEL.getCode());
        marketingCouponBO.setCreateTime(new Date());
        marketingCouponBO.setUpdateTime(new Date());
        int add = marketingCouponService.add(marketingCouponBO);
        return marketingCouponBO;
    }


    /**
     * 更新
     * @param marketingCouponBO
     * @return
     */
    public MarketingCouponBO update(MarketingCouponBO marketingCouponBO) {
        marketingCouponBO.setDelFlag(DelFlagEnum.NOT_DEL.getCode());
        int add = marketingCouponService.update(marketingCouponBO);
        return marketingCouponBO;
    }

    /**
     * 查询已启用未过期优惠券
     * @param id
     * @return
     */
    public MarketingCouponBO findEffective(Long id) {
        MarketingCouponQuery query = new MarketingCouponQuery();
        query.setId(id);
        query.setCouponStatus(CouponStatusEnum.ENABLE.getCode());
        query.setFindDate(new Date());
        query.setPageNum(1);
        query.setPageSize(1);
        PageListResult<MarketingCouponBO> page = this.findPage(query);
        if(!ObjectUtils.isEmpty(page) && !CollectionUtils.isEmpty(page.getList())){
            return page.getList().get(0);
        }
        return null;
    }

    /**
     * 查询已启用未过期优惠券
     * @param ids
     * @return
     */
    public List<MarketingCouponBO> findEffectiveSet(Set<Long> ids) {
        MarketingCouponQuery query = new MarketingCouponQuery();
        query.setIds(ids);
        query.setCouponStatus(CouponStatusEnum.ENABLE.getCode());
        query.setFindDate(new Date());
        List<MarketingCouponBO> page = this.findList(query);

        return page;
    }

}
